package com.aruba.hiltapplication

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.aruba.daggerapplication.api.BaiduApiService
import com.aruba.hiltapplication.di.datasource.DataSource
import dagger.hilt.android.AndroidEntryPoint
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

    @Inject
    lateinit var dataSource: DataSource

    @Inject
    lateinit var baiduApiService1: BaiduApiService

    @Inject
    lateinit var baiduApiService2: BaiduApiService

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        Log.i("aruba_log", dataSource.toString())
        Log.i("aruba_log", "baiduApiService1 hashcode:${baiduApiService2.hashCode()}")
        Log.i("aruba_log", "baiduApiService2 hashcode:${baiduApiService2.hashCode()}")

        getIndex()

        findViewById<TextView>(R.id.tv_hello).setOnClickListener {
            startActivity(Intent(this@MainActivity, ViewModelActivity::class.java))
        }
    }

    /**
     * 获取百度首页
     */
    private fun getIndex() {
        baiduApiService1.index().enqueue(object : Callback<String> {
            override fun onResponse(call: Call<String>, response: Response<String>) {
                findViewById<TextView>(R.id.tv_hello).text = response.body()
            }

            override fun onFailure(call: Call<String>, t: Throwable) {
            }
        })
    }
}